package com.py.credit.service.impl;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.py.credit.dao.ContentStyleInfoDao;
import com.py.credit.dao.SContentDao;
import com.py.credit.entity.Column;
import com.py.credit.entity.ContentStyle;
import com.py.credit.entity.ContentStyleInfo;
import com.py.credit.entity.TableResult;
import com.py.credit.service.ContentStyleService;

@Service
public class ContentStyleServiceImpl implements ContentStyleService {

	@Autowired
	private ContentStyleInfoDao contentStyleInfoDao;

	@Autowired
	private SContentDao sContentDao;

	@Transactional
	@Override
	public void update(String contentId, List<ContentStyle> contentStyles) {

		contentStyleInfoDao.deleteContentStyle(contentId);
		contentStyleInfoDao.addContentStyle(contentStyles, contentId);

	}

	@Override
	public List<TableResult> getDetail(String contentId, String type, String kId) {

		List<TableResult> tableResults = sContentDao.selectSsearchResource(kId, type);

		List<ContentStyle> contentStyles = contentStyleInfoDao.selectContentStyleByContentId(contentId);

		if (contentStyles != null && contentStyles.size() > 0) {

			for (TableResult tableResult : tableResults) {

				for (ContentStyle contentStyle : contentStyles) {

					if (tableResult.getTableId().equals(String.valueOf(contentStyle.getTableId()))) {

						if (StringUtils.isNotBlank(contentStyle.getTableStyle())) {
							tableResult.setStyleSheet(contentStyle.getTableStyle());
							tableResult.setColumns(null);
							tableResult.setData(null);
						}

						break;

					}

				}

			}
		}

		for (TableResult tableResult : tableResults) {

			String showType = tableResult.getShowType();

			int i = 0;

			if (StringUtils.isBlank(tableResult.getStyleSheet())) {

				List<Column> columns = tableResult.getColumns();

				int size = columns.size();

				StringBuffer sb = new StringBuffer();

				if ("S".equals(showType)) {

					if ("YZS".equals(type)) {

						sb.append("<table class='singleTable'><tbody>");

						for (Column column : columns) {

							String metricCode = column.getMetricCode();

							String metricName = column.getMetricName();

							if (i % 2 == 0) {

								sb.append("<tr>");

							}

							sb.append("<td>" + metricName + "</td><td id='" + metricCode + "'></td>");

							if (i % 2 != 0 || i == size - 1) {

								sb.append("</tr>");

							}

							i++;

						}

						sb.append("</tbody></table>");

					} else {

						sb.append("<table><tbody>");

						for (Column column : columns) {

							String metricCode = column.getMetricCode();

							String metricName = column.getMetricName();

							sb.append("<tr><td>" + metricName + "</td><td id='" + metricCode + "'></tr></td>");

						}

						sb.append("</tbody></table>");

					}

				} else {

					if ("YZS".equals(type)) {

						sb.append("<table><tbody><tr>");

						for (Column column : columns) {

							String metricCode = column.getMetricCode();

							String metricName = column.getMetricName();

							sb.append("<th id='" + metricCode + "'>" + metricName + "</th>");

						}

						sb.append("</tr></tbody></table>");

					} else {

						sb.append("<table><tbody>");

						for (Column column : columns) {

							String metricCode = column.getMetricCode();

							String metricName = column.getMetricName();

							sb.append("<tr><td>" + metricName + "</td><td id='" + metricCode + "'></td></tr>");

						}

						sb.append("</tbody></table>");

					}

				}

				tableResult.setStyleSheet(sb.toString());

			}

			tableResult.setColumns(null);
			tableResult.setData(null);

		}

		return tableResults;
	}

	@Override
	public List<ContentStyleInfo> getTableList() {

		return contentStyleInfoDao.selectContentStyleInfo();

	}

	@Override
	public List<ContentStyle> getByType(String type) {

		return contentStyleInfoDao.selectByType(type);
	}

	@Override
	public ContentStyle getStyle(String contentId, String tableId) {
		// TODO Auto-generated method stub
		return contentStyleInfoDao.selectByTableId(contentId, tableId);
	}

}
